package com.leetcode;

import com.leetcode.common.ListNode;

/**
 * 24. 两两交换链表中的节点
 * 递归
 */
public class Solution24_1 {

    /**
     * 简化代码版
     *
     * @param head
     * @return
     */
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode newHead = head.next;
        head.next = swapPairs(newHead.next);
        newHead.next = head;
        return newHead;
    }

    public ListNode swapPairs1(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode node1 = head;
        ListNode node2 = node1.next;

        ListNode newHead = swapPairs1(node2.next);

        node2.next = node1;
        node1.next = newHead;

        return node2;
    }

}
